package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.article.domain.Flight;
import cn.wolfcode.luowowo.article.query.AirTicketQuery;
import cn.wolfcode.luowowo.article.service.IFlightService;
import cn.wolfcode.luowowo.cache.service.IAirTicketRedisService;
import cn.wolfcode.luowowo.common.util.DateToWeek;
import cn.wolfcode.luowowo.common.util.ListRemoveRepetition;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.webSite.annotation.RequireLogin;
import cn.wolfcode.luowowo.webSite.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("flight")
public class AirTicketController {

    @Reference
    private IFlightService flightService;

    @Reference
    private IAirTicketRedisService airTicketRedisService;

    @RequestMapping("")
    public String index(Model model) {

        return "flight/index";
    }

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") AirTicketQuery qo, Model model,@UserParam UserInfo userInfo) {

        if(qo.getDepartCityName()==null&&qo.getDestCityName()==null){
            return "flight/index";
        }

        //分页查询
        PageInfo<Flight> pageInfo = flightService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //下拉框显示
        List<Flight> list = pageInfo.getList();
        querySome(list,model);


        //历史查询保存到Redis
        if(userInfo != null) {
            airTicketRedisService.save(qo.getDepartCityName(), qo.getDestCityName(), qo.getStartDate(), userInfo.getId());
            queryHistory(model,userInfo);
        }



        return "flight/list";
    }

    //机票订单
    @RequestMapping("order")
    @RequireLogin
    public String order(Long id,@ModelAttribute("qo") AirTicketQuery qo, Model model, @UserParam UserInfo userInfo) {
        Flight flight = flightService.get(id);
        model.addAttribute("flight", flight);
        model.addAttribute("userInfo", userInfo);

        return "flight/order";
    }

    @RequestMapping("/filtrate")
    public String travels(Model model, @ModelAttribute("qo") AirTicketQuery qo,@UserParam UserInfo userInfo){
        PageInfo<Flight> pageInfo = flightService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询机场,机型,航空公司
        List<Flight> flights = flightService.querySome(qo);
        querySome(flights,model);

       /* String air = qo.getAirlines();
        model.addAttribute("air",air );*/

        //查询历史
        queryHistory(model,userInfo);

        return "flight/list";
    }

    //下拉框显示
    private static void querySome(List<Flight> list,Model model){

        //起飞机场
        List objects1 = new ArrayList<>();
        for (Flight flight : list) {
            String departureAirport = flight.getDepartureAirport();
            objects1.add(departureAirport);
        }
        List departureAirports = ListRemoveRepetition.removeDuplicate(objects1);
        model.addAttribute("departureAirports", departureAirports);

        //航空公司
        List objects2 = new ArrayList<>();
        for (Flight flight : list) {
            String airlines = flight.getAirlines();
            objects2.add(airlines);
        }
        List Airlines = ListRemoveRepetition.removeDuplicate(objects2);
        model.addAttribute("Airlines", Airlines);

        //机型
        List objects3 = new ArrayList<>();
        for (Flight flight : list) {
            String aircraftModels = flight.getAircraftModels();
            objects3.add(aircraftModels);
        }
        List AircraftModels = ListRemoveRepetition.removeDuplicate(objects3);
        model.addAttribute("AircraftModels", AircraftModels);

    }

    //历史查询显示
    private  void queryHistory(Model model,UserInfo userInfo){
        List<String> departCitys = airTicketRedisService.listDepartCityName(userInfo.getId());
        List<String> destCitys = airTicketRedisService.listDestCityName(userInfo.getId());
        List<String> startDates = airTicketRedisService.listStartDate(userInfo.getId());

        //历史查询第一列显示
        String departCity1 = departCitys.get(1);
        String destCity1 = destCitys.get(1);
        String startDate1 = startDates.get(1);
        String week1 = DateToWeek.dateToWeek(startDate1);

        model.addAttribute("departCity1",departCity1);
        model.addAttribute("destCity1",destCity1);
        model.addAttribute("startDate1",startDate1);
        model.addAttribute("week1",week1);

        //历史查询第二列显示
        String departCity2 = departCitys.get(2);
        String destCity2 = destCitys.get(2);
        String startDate2 = startDates.get(2);
        String week2 = DateToWeek.dateToWeek(startDate2);

        model.addAttribute("departCity2",departCity2);
        model.addAttribute("destCity2",destCity2);
        model.addAttribute("startDate2",startDate2);
        model.addAttribute("week2",week2);
    }

    @RequestMapping("buy")
    public String buy(@ModelAttribute("qo") AirTicketQuery qo){

        return "/flight/buy";
    }
}


